/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-07-31 09:27:11
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-08-14 17:25:22
 * @FilePath: \JS32T031_SDK\User\board\433_remote_control.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "board/433_remote_control.h"
#include "433_decoding/433_software_decoding.h"
/** @addtogroup Template_Project
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define WIRELESS_433_TIMx TIMER0
#define TIMERx_IRQn TIM0_IRQn
#define WIRELESS_433_Px PB
#define WIRELESS_433_PINx PIN_9

void wireless_timer_init(void)
{
    NVIC_InitTypeDef nvic; // 使能TIMER0中断
    nvic.NVIC_IRQChannel = TIMERx_IRQn;
    nvic.NVIC_IRQChannelPriority = 0;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    TYPE_LL_TIMER_INIT timer_init;

    timer_init.prescaler = LL_TIMER_PSC_8;              // 不分频
    timer_init.timer_src_sel = LL_TIMER_SRC_SYS_RISING; // 选择系统时钟
    ll_timer_init(WIRELESS_433_TIMx, &timer_init);

    TYPE_LL_TIMER_CNT_CFG cnt_cfg;
    cnt_cfg.count_initial = 0;
    cnt_cfg.count_period = 48 * WIRELESS_433_TASK_TIMESTAMP / 8; // 100ms   48Mhz*100000us/128 = 37,500         48Mhz*50us/8 = 300
    cnt_cfg.count_ie = ENABLE;
    ll_timer_cnt_mode_config(WIRELESS_433_TIMx, &cnt_cfg);
    ll_timer_start(WIRELESS_433_TIMx, LL_TIMER_MODE_SEL_COUNTER);
}

unsigned char get_433_io_state(void)
{
    volatile unsigned char io_state = ll_gpio_read_input_data_bit(WIRELESS_433_Px, WIRELESS_433_PINx);
    // printf("%u",io_state);
    return io_state;
}
void wireless_433_Config(void)
{
    TYPE_LL_GPIO_INIT gpio_struct;
    memset(&gpio_struct, 0x00, sizeof(gpio_struct));
    gpio_struct.gpio_pin = WIRELESS_433_PINx;
    gpio_struct.gpio_mode = LL_GPIO_MODE_IN;   // 输入模式
    gpio_struct.gpio_pupd = LL_GPIO_PUPD_DOWN; // 下拉
    ll_gpio_init(WIRELESS_433_Px, &gpio_struct);

    wireless_433_config_t wireless_433_Config;
    wireless_433_Config.get_io = get_433_io_state;
    decode_433_io_init(&wireless_433_Config);

    wireless_timer_init();
}
